package com.jzo2o.customer.controller.consumer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Address;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: chy
 * \* Date: 2025/9/26  14:45
 * \* Description:
 * \
 */
@RestController("CommonUserAddressBook")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址薄接口")
@Slf4j
public class CommonUserAddressBook {
    @Resource
    private IAddressBookService addressBookService;

    /**
     * 添加地址薄
     *
     * @param reqDTO
     */
    @PostMapping
    public void addAddressBook (@RequestBody AddressBookUpsertReqDTO reqDTO) {
        log.info("添加地址薄:{}", reqDTO);
        addressBookService.addAddressBook(reqDTO);

    }
    /**
     * 获取地址薄列表
     *
     * @param queryReqDTO
     * @return
     */
    @GetMapping("/page")
    public PageResult<AddressBook> getList(AddressBookPageQueryReqDTO queryReqDTO) {
        log.info("获取地址薄列表:{}", queryReqDTO);
        PageResult<AddressBook> addressBooks = addressBookService.getAddressBooks(queryReqDTO);
        return addressBooks;
    }

    /**
     * 获取地址薄
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AddressBook getById(@PathVariable Long id) {
        log.info("获取地址薄:{}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return addressBook;
    }
    /**
     * 删除地址薄
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Boolean deleteById(@RequestBody List< Long> ids) {
        log.info("删除地址薄:{}", ids);
        return addressBookService.removeByIds(ids);
    }
    /**
     * 修改地址薄
     * @param reqDTO
     * @return
     */
    @PutMapping("/{id}")
    public void updateById(@PathVariable Long id, @RequestBody AddressBookUpsertReqDTO reqDTO) {
        log.info("修改地址薄:{}", reqDTO);
        addressBookService.updateAddressById(id, reqDTO);
    }

    /**
     * 设置默认地址
     * @param id
     * @param flag
     */
    @PutMapping("/default")
    public void updateDefault(@RequestParam Long id, @RequestParam Integer flag) {
        log.info("设置默认地址:id={}, flag={}", id, flag);
        // 调用service层处理逻辑
        addressBookService.updateDefault(id, flag);
    }

}
